package com.ztesoft.report.controller;


import com.ztesoft.report.entity.UploadLog;
import com.ztesoft.report.service.ReportDownloadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 谭宏伟
 * @since 2018-06-13
 */
@RestController
@RequestMapping("/reportDownload")
public class ReportDownloadController{
    @Autowired
    ReportDownloadService reportDownloadService;

    /**
     * 获取服务器列表
     * @return
     */
    @RequestMapping("/getServers")
    public Object getServers() {
        return reportDownloadService.getServers();
    }

    /**
     * 获取分类
     * @return
     */
    @RequestMapping("/getCategory")
    public Object getCategory() {
        return reportDownloadService.getCategory();
    }

    @RequestMapping("/list")
    public Object list(@RequestBody Map<String,Object> map){
        return reportDownloadService.queryPage(map);
    }

    /**
     * 从服务器上把文件下载到运行服务器，为下一步下载到本机做准备
     * @return
     */
    @RequestMapping("/downLoadFile")
    public Object downLoadFile(@RequestBody UploadLog uploadLog) {
        return reportDownloadService.downLoadFile(uploadLog);
    }

    /**
     * 下载报表到本机
     * @param response
     * @param request
     */
    @RequestMapping(method = RequestMethod.GET, value = "/downLoadLocal")
    public void downLoadLocal(HttpServletResponse response,
                                HttpServletRequest request) {
        reportDownloadService.downLoadLocal(response,request);
    }

    /**
     * 下载报表到本机
     * @param response
     * @param request
     */
    @RequestMapping(method = RequestMethod.GET, value = "/downLoadLocalPage2")
    public void downLoadLocalPage2(HttpServletResponse response,
                              HttpServletRequest request) {
        String createPath = request.getParameter("createPath");
        String filename = request.getParameter("filename");
        try {
            response.setCharacterEncoding("utf-8");
            response.setContentType("multipart/form-data");
            response.setHeader("Content-Disposition",
                    "attachment;fileName=" + java.net.URLEncoder.encode(filename, "utf-8"));
            File file = new File(createPath);
            response.setContentLength(new Long(file.length()).intValue());
            InputStream inputStream = new FileInputStream(file);
            OutputStream os = response.getOutputStream();
            byte[] b = new byte[2048];
            int length;
            while ((length = inputStream.read(b)) > 0) {
                os.write(b, 0, length);
            }
            os.close();
            inputStream.close();
            file.delete();
        }catch (Exception e){
            e.printStackTrace();
        }

    }

}
